<!--
 * @Author: ૮₍ ˙О˙₎ა
 * @Date: 2023-07-18 20:59:04
-->
<template>
  <el-card>
    <template #header>
      <el-button type="primary" :icon="Plus" @click="addEditTrademark()">添加</el-button>
    </template>

    <el-table :data="stateList.trademarkList" border stripe>
      <el-table-column type="index" label="序号" width="80" />
      <el-table-column prop="tmName" label="品牌名称" />
      <el-table-column prop="logoUrl" label="品牌LOGO">
        <template #default="{ row }">
          <img :src="row.logoUrl" alt="" style="width: 100px" />
        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作" width="180">
        <template #default="{ row }">
          <el-button :icon="Edit" type="warning" @click="addEditTrademark(row)">修改</el-button>
          <el-button :icon="Delete" type="danger" @click="delTrademark(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:current-page="stateList.page"
      v-model:page-size="stateList.limit"
      :page-sizes="[5, 10, 15, 20]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="stateList.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <el-dialog
      v-model="dialogVisible"
      @close="closeDialog"
      align-center
      :title="isShow ? '添加' : '编辑'"
      width="50%"
    >
      <el-form :model="reqAddTrademarkParams" label-width="80px">
        <el-form-item label="品牌名称">
          <el-input v-model="reqAddTrademarkParams.tmName" />
        </el-form-item>
        <el-form-item label="品牌 Logo">
          <el-upload
            class="avatar-uploader"
            action="/api/admin/product/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img
              v-if="reqAddTrademarkParams.logoUrl"
              :src="reqAddTrademarkParams.logoUrl"
              class="avatar"
            />
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="reqAddEmitTrademark"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>

<script lang="ts" setup>
import { reqUpdateBaseTrademark } from '@/api/trademark'
import { reqAddBaseTrademarkRes } from '@/api/trademark'
import { reqRemoveRes } from '@/api/trademark'
import { reqBaseTrademark, type BaseTrademarkItem } from '@/api/trademark'
import { Delete, Edit, Plus } from '@element-plus/icons-vue'
import { ElMessage, type UploadProps } from 'element-plus'
import { onMounted, ref } from 'vue'
const dialogVisible = ref(false)

const reqAddTrademarkParams = ref({
  logoUrl: '',
  tmName: ''
})

const isShow = ref(true)

const delTrademark = async (id: number) => {
  const res = await reqRemoveRes(id)
  if (res.code === 200) {
    ElMessage.success('删除成功')
    getTrademarkList()
  }
}

const reqAddEmitTrademark = async () => {
  const res = isShow.value
    ? await reqAddBaseTrademarkRes(reqAddTrademarkParams.value)
    : await reqUpdateBaseTrademark(reqAddTrademarkParams.value)

  if (res.code === 200) {
    ElMessage.success(isShow.value ? '添加成功' : '编辑成功')
    dialogVisible.value = false
  }
  getTrademarkList()
}

const addEditTrademark = (row?: BaseTrademarkItem) => {
  dialogVisible.value = true
  isShow.value = true
  if (row?.id) {
    isShow.value = false

    reqAddTrademarkParams.value = row
  }
}

const handleSizeChange = (val: number) => {
  stateList.value.limit = val
  getTrademarkList()
}
const handleCurrentChange = (val: number) => {
  stateList.value.page = val
  getTrademarkList()
}
const handleAvatarSuccess: UploadProps['onSuccess'] = (response, uploadFile) => {
  reqAddTrademarkParams.value.logoUrl = URL.createObjectURL(uploadFile.raw!)
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('Avatar picture must be JPG format!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 10) {
    ElMessage.error('Avatar picture size can not exceed 2MB!')
    return false
  }
  return true
}
const closeDialog = () => {
  reqAddTrademarkParams.value = {
    logoUrl: '',
    tmName: ''
  }
}

interface reqTrademarkRecord {
  id: number
  logoUrl: string
  tmName: string
}
interface trademarkState {
  page: number
  limit: number
  total: number
  trademarkList: reqTrademarkRecord[]
}
const stateList = ref<trademarkState>({
  page: 1,
  limit: 10,
  total: 0,
  trademarkList: []
})
const getTrademarkList = async () => {
  const res = await reqBaseTrademark(stateList.value.page, stateList.value.limit)
  if (res.code === 200) {
    stateList.value.trademarkList = res.data.records
    stateList.value.total = res.data.total
  }
}
onMounted(() => {
  getTrademarkList()
})
</script>

<script lang="ts">
export default {
  name: 'TrademarkView'
}
</script>
<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
